﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vikipedia.VikiPlayer.WPF.Controls
{
    /// <summary>
    /// Interaction logic for ControlPanel.xaml
    /// </summary>
    public partial class ControlPanel : UserControl
    {
        static public readonly DependencyProperty TargetMediaElementProperty =
            DependencyProperty.Register
            ("TargetMediaElement", 
            typeof(MediaElement), 
            typeof(ControlPanel),
            new PropertyMetadata
                ((MediaElement)null,
                    new PropertyChangedCallback (                         
                        (d, e) =>
                            {
                                var cp = d as ControlPanel;
                                if (cp!=null)
                                    cp.TargetMediaElement_Changed(d, e);
                            }
                        )
                ) );

        public MediaElement TargetMediaElement
        {
            get
            {
                return   this.GetValue(TargetMediaElementProperty) as MediaElement;
            }
            set
            {              
                this.SetValue (TargetMediaElementProperty,value );
            }
        }
        void TargetMediaElement_Changed (object  o, DependencyPropertyChangedEventArgs e  )
        {
            var ov = e.OldValue as MediaElement ;
            var nv = e.NewValue as MediaElement;
            if (nv != null)
            {
                nv.MediaOpened += new RoutedEventHandler(ov_MediaOpened);
                nv.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(nv_MediaFailed);
                nv.MediaEnded += new RoutedEventHandler(nv_MediaEnded);
                
            }
            if (ov != null)
            {
                ov.MediaOpened -= ov_MediaOpened;
                ov.MediaFailed -= nv_MediaFailed;
                ov.MediaEnded -= nv_MediaEnded;
            
            }
        
        }

        void nv_MediaEnded(object sender, RoutedEventArgs e)
        {
           // throw new NotImplementedException();
        }

        void nv_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
         //   throw new NotImplementedException();
        }

        void ov_MediaOpened(object sender, RoutedEventArgs e)
        {
           // throw new NotImplementedException();
        }


        public ControlPanel()
        {
            InitializeComponent();
 
 
        }
    }
}
